Micron Document
Livres et Wikis | Archives | Info


JavaScript syntax
part 22/31 Β· 107.4 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
β€’ There are differences between the various Web browsers with regard to which properties will be reflected with the for...in loop statement. In theory, this is controlled by an internal state property defined by the ECMAscript standard called "DontEnum", but in practice, each browser returns a slightly different set of properties during introspection. It is useful to test for a given property using if (some_object.hasOwnProperty(property_name)) { ... }. Thus, adding a method to the array prototype with Array.prototype.newMethod = function() {...} may cause for ... in loops to loop over the method's name.

While loop

The syntax of the JavaScript while loop is as follows:

while (condition) {
statement1;
statement2;
statement3;
...
}

Do ... while loop

The syntax of the JavaScript do ... while loop is as follows:

do {
statement1;
statement2;
statement3;
...
} while (condition);

With

The with statement adds all of the given object's properties and methods into the following block's scope, letting them be referenced as if they were local variables.

with (document) {
const a = getElementById('a');
const b = getElementById('b');
const c = getElementById('c');
};

β€’ Note the absence of document. before each getElementById() invocation.

The semantics are similar to the with statement of Pascal.

Because the availability of with statements hinders program performance and is believed to reduce code clarity (since any given variable could actually be a property from an enclosing with), this statement is not allowed in strict mode.

Labels

JavaScript supports nested labels in most implementations. Loops or blocks can be labeled for the break statement, and loops for continue. Although goto is a reserved word,cite-ref-19[19] goto is not implemented in JavaScript.

loop1: for (let a = 0; a < 10; ++a) {
if (a === 4) break loop1; // Stops after the 4th attempt
console.log('a = ' + a);
loop2: for (let b = 0; b < 10; ++b) {
if (b === 3) continue loop2; // Number 3 is skipped
if (b === 6) continue loop1; // Continues the first loop, 'finished' is not shown
console.log('b = ' + b);
} //end of loop2
console.log('finished');
} //end of loop1
block1: {
console.log('Hello'); // Displays 'Hello'
break block1;
console.log('World'); // Will never get here
}
goto block1; // Parse error.


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────